package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.api.domain.CldStaff;
import com.cleaningcloud.cld.domain.*;
import com.cleaningcloud.cld.domain.dto.CldWelfareSendDto;
import com.cleaningcloud.cld.domain.vo.CldStaffVo;
import com.cleaningcloud.cld.domain.vo.CldWelfareSendDetailsVo;
import com.cleaningcloud.cld.domain.vo.CldWelfareSendVo;
import com.cleaningcloud.cld.mapper.*;
import com.cleaningcloud.cld.service.ICldPayDetailsService;
import com.cleaningcloud.cld.service.ICldWelfareSendService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 福利管理Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-11-08
 */
@Service
public class CldWelfareSendServiceImpl implements ICldWelfareSendService {
    @Autowired
    private CldWelfareSendMapper cldWelfareSendMapper;

    @Autowired
    private CldProjectPostMapper projectPostMapper;

    @Autowired
    private CldWelfareMapper cldWelfareMapper;

    @Autowired
    private ICldPayDetailsService cldPayDetailsService;

    @Autowired
    private CldContractMapper contractMapper;

    @Autowired
    private CldWelfareSendDetailsMapper cldWelfareSendDetailsMapper;

    @Autowired
    private CldStaffMapper cldStaffMapper;

    /**
     * 查询福利管理
     *
     * @param welfareSendId 福利管理主键
     * @return 福利管理
     */
    @Override
    public CldWelfareSend selectCldWelfareSendByWelfareSendId(Long welfareSendId) {
        return cldWelfareSendMapper.selectCldWelfareSendByWelfareSendId(welfareSendId);
    }

    /**
     * 查询福利管理列表
     *
     * @param cldWelfareSend 福利管理
     * @return 福利管理
     */
    @Override
    public List<CldWelfareSendVo> selectCldWelfareSendList(CldWelfareSendDto cldWelfareSend) {
        return cldWelfareSendMapper.selectCldWelfareSendList(cldWelfareSend);
    }

    @Override
    public List<CldWelfareSendDetailsVo> selectCldWelfareSendDetailsList(CldWelfareSendDetails cldWelfareSendDetails) {
        return cldWelfareSendDetailsMapper.selectCldWelfareSendDetailsList(cldWelfareSendDetails);
    }

    /**
     * 新增福利管理
     *
     * @param cldWelfareSend 福利管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCldWelfareSend(CldWelfareSendDto cldWelfareSend) {
        cldWelfareSend.setCreateTime(DateUtils.getNowDate());
        cldWelfareSend.setCreateId(SecurityUtils.getUserId());
        cldWelfareSend.setCreateName(SecurityUtils.getUsername());
        cldWelfareSend.setFlag(0L);
        cldWelfareSend.setSendTime(DateUtils.getNowDate());
        // 发放项目类型为所有项目时
        if (cldWelfareSend.getProjectType() == 1) {
            // 查询签订中的项目
            CldContract query = new CldContract();
            query.setContractStatus(1L);
            List<CldContract> cldContractList = contractMapper.selectCldContractList(query);
            for (CldContract contract : cldContractList) {
                cldWelfareSend.setProjectId(contract.getProjectId());
                insertWelfareSend(cldWelfareSend);
            }
        } else {
            if (!cldWelfareSend.getProjectList().isEmpty()) {
                for (CldProject cldProject : cldWelfareSend.getProjectList()) {
                    cldWelfareSend.setProjectId(cldProject.getProjectId());
                    insertWelfareSend(cldWelfareSend);
                }
            }

        }
        return 1;
    }

    public void insertWelfareSend(CldWelfareSendDto cldWelfareSend) {
        cldWelfareSendMapper.insertCldWelfareSend(cldWelfareSend);
        // 查询项目岗位 所有在岗员工
        CldProjectPost postQuery = new CldProjectPost();
        postQuery.setProjectId(cldWelfareSend.getProjectId());
        List<CldProjectPost> cldProjectPosts = projectPostMapper.selectPostStaff(postQuery);
        if (!cldProjectPosts.isEmpty()) {
            for (CldProjectPost projectPost : cldProjectPosts) {
                if(null != projectPost.getStaffId()) {
                    CldStaffVo staff = cldStaffMapper.selectCldStaffByStaffId(projectPost.getStaffId());
                    if(null != staff){
                        //过滤已挂岗人员
                        if(null == staff.getIsPrepared() || staff.getIsPrepared() == 0){
                            CldWelfareSendDetails cldWelfareSendDetails = new CldWelfareSendDetails();
                            cldWelfareSendDetails.setSendMoney(cldWelfareSend.getMoney());
                            cldWelfareSendDetails.setWelfareSendId(cldWelfareSend.getWelfareSendId());
                            cldWelfareSendDetails.setStaffId(projectPost.getStaffId());
                            cldWelfareSendDetailsMapper.insertCldWelfareSendDetails(cldWelfareSendDetails);
                            // 薪酬明细
                            CldPayDetails cldPayDetails = new CldPayDetails();
                            cldPayDetails.setBusinessId(cldWelfareSendDetails.getWelfareSendDetailsId());
                            cldPayDetails.setProjectId(cldWelfareSend.getProjectId());
                            cldPayDetails.setStaffId(projectPost.getStaffId());
                            cldPayDetails.setProjectPostId(projectPost.getProjectPostName());
                            cldPayDetails.setPayType(3L);
                            cldPayDetails.setMoney(cldWelfareSendDetails.getSendMoney());
                            cldPayDetails.setIsAlgorithm(1L);
                            CldWelfare cldWelfare = cldWelfareMapper.selectCldWelfareByWalfareId(cldWelfareSend.getWalfareId());
                            if(null != cldWelfare){
                                cldPayDetails.setRemark(cldWelfare.getWalfareName());
                            }
                            cldPayDetailsService.insertCldPayDetails(cldPayDetails);
                        }
                    }
                }
            }
        }
    }

    /**
     * 修改福利管理
     *
     * @param cldWelfareSend 福利管理
     * @return 结果
     */
    @Override
    public int updateCldWelfareSend(CldWelfareSendDto cldWelfareSend) {
        return cldWelfareSendMapper.updateCldWelfareSend(cldWelfareSend);
    }

    @Override
    @Transactional
    public int updateCldWelfareSendDetails(CldWelfareSendDetails cldWelfareSendDetails) {

        // 查询薪酬明细
        CldPayDetails cldPayDetails = cldPayDetailsService.selectCldPayDetailsByBusinessId(cldWelfareSendDetails.getWelfareSendDetailsId());
        if (cldPayDetails != null) {
            cldPayDetails.setMoney(cldWelfareSendDetails.getSendMoney());
            cldPayDetailsService.updateCldPayDetails(cldPayDetails);
        }
        return cldWelfareSendDetailsMapper.updateCldWelfareSendDetails(cldWelfareSendDetails);
    }

    /**
     * 批量删除福利管理
     *
     * @param welfareSendIds 需要删除的福利管理主键
     * @return 结果
     */
    @Override
    public int deleteCldWelfareSendByWelfareSendIds(Long[] welfareSendIds) {
        return cldWelfareSendMapper.deleteCldWelfareSendByWelfareSendIds(welfareSendIds);
    }

    /**
     * 删除福利管理信息
     *
     * @param welfareSendId 福利管理主键
     * @return 结果
     */
    @Override
    public int deleteCldWelfareSendByWelfareSendId(Long welfareSendId) {
        return cldWelfareSendMapper.deleteCldWelfareSendByWelfareSendId(welfareSendId);
    }
}
